package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class SliderBar extends View {

    private String[] sliderBar = { "热门", "A", "B", "C", "D", "E", "F", "G", "H", "J",
            "K", "L", "M", "N", "Q", "S", "T", "W", "X", "Y", "Z" ,"#"};
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    int state;
    public SliderBar(Context context) {
        super(context);
    }

    public SliderBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public SliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private OnTouchLetterChangedListener listener;
    public interface OnTouchLetterChangedListener{
        void onTouchLetterChanged(String s);
    }
    public void setOnTouchLetterChangedListener(OnTouchLetterChangedListener listener){
        this.listener = listener;
    }
    private void initPaint(){
        paint.setColor(Color.BLACK);//设置画笔颜色为黑色
        paint.setTypeface(Typeface.DEFAULT_BOLD);//默认字体为粗体
        paint.setAntiAlias(true);//反锯齿
        paint.setTextSize(30);//设置要绘制的文字大小
        paint.setTextAlign(Paint.Align.CENTER);//设置绘制的起点为中心
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.AT_MOST) {
            widthSize = 90;
        }
        setMeasuredDimension(widthSize,heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int height = getHeight();//当前view的高度，用于计算每个文字占用的高度
        int width = getWidth();//当前viewde宽度
        int eachHeight = height/sliderBar.length;//每个字母的高度 = 总高度/文字个数
        for (int i = 0 ; i < sliderBar.length; i++) {
            Rect bounds = new Rect();
            paint.getTextBounds(sliderBar[i],0,sliderBar[i].length(),bounds);
            float x = width/2;
            float y = eachHeight/2+ bounds.height()/2+ i*eachHeight;
            canvas.drawText(sliderBar[i],x,y,paint);//根据x,y坐标画出当前文字
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        int index = (int) (y/getHeight()*sliderBar.length);
        switch (action){
            case MotionEvent.ACTION_UP:
                setBackgroundResource(android.R.color.transparent);
                invalidate();
                break;
            default:
                setBackgroundResource(R.drawable.text_index);
                if(index>0&&index<sliderBar.length){
                    invalidate();
                    if(listener!=null){
                        listener.onTouchLetterChanged(sliderBar[index]);
                    }
                }
                break;
        }
        //注意，这里必须要返回true，目的就是不让listview响应触摸事件，因为我们要通过滑动控制listview的显示
        return true;
}

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable parcelable = super.onSaveInstanceState();
        SliderBarSavedState sliderBarSavedState = new SliderBarSavedState(parcelable);
        sliderBarSavedState.state = state;
        return sliderBarSavedState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SliderBarSavedState sliderBarSavedState = (SliderBarSavedState) state;
        super.onRestoreInstanceState(sliderBarSavedState.getSuperState());
        this.state = sliderBarSavedState.state;
    }


    static class SliderBarSavedState extends BaseSavedState {

        int state;


        public SliderBarSavedState(Parcel source) {
            super(source);
            state = source.readInt();
        }

        public SliderBarSavedState(Parcelable superState) {
            super(superState);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(state);
        }

        public static final Parcelable.Creator<SliderBarSavedState> CREATOR
                = new Parcelable.Creator<SliderBarSavedState>() {
            public SliderBarSavedState createFromParcel(Parcel in) {
                return new SliderBarSavedState(in);
            }

            public SliderBarSavedState[] newArray(int size) {
                return new SliderBarSavedState[size];
            }
        };
    }
}
